package com.hiultra.archive.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hiultra.archive.domain.ArchiveStocktakingTask;
import com.hiultra.archive.service.IArchiveStocktakingTaskService;
import com.hiultra.common.annotation.Log;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.core.page.TableDataInfo;
import com.hiultra.common.enums.BusinessType;
import com.hiultra.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.List;

/**
 * 盘点任务Controller
 *
 * @author paidaxing
 * @date 2024-07-31
 */
@RestController
@RequestMapping("/archive/ArchiveStocktakingTask")
public class ArchiveStocktakingTaskController extends BaseController
{
    @Autowired
    private IArchiveStocktakingTaskService archiveStocktakingTaskService;

    /**
     * 查询盘点任务列表
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveStocktakingTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArchiveStocktakingTask archiveStocktakingTask) throws JsonProcessingException, ParseException {
        startPage();
        List<ArchiveStocktakingTask> list = archiveStocktakingTaskService.selectArchiveStocktakingTaskList(archiveStocktakingTask);
        return getDataTable(list);
    }

    /**
     * 导出盘点任务列表
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveStocktakingTask:export')")
    @Log(title = "盘点任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArchiveStocktakingTask archiveStocktakingTask) throws JsonProcessingException, ParseException {
        List<ArchiveStocktakingTask> list = archiveStocktakingTaskService.selectArchiveStocktakingTaskList(archiveStocktakingTask);
        ExcelUtil<ArchiveStocktakingTask> util = new ExcelUtil<ArchiveStocktakingTask>(ArchiveStocktakingTask.class);
        util.exportExcel(response, list, "盘点任务数据");
    }

    /**
     * 获取盘点任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveStocktakingTask:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") String taskId)
    {
        return AjaxResult.success(archiveStocktakingTaskService.selectArchiveStocktakingTaskByTaskId(taskId));
    }

    /**
     * 新增盘点任务
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveStocktakingTask:add')")
    @Log(title = "盘点任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArchiveStocktakingTask archiveStocktakingTask)
    {
        return toAjax(archiveStocktakingTaskService.insertArchiveStocktakingTask(archiveStocktakingTask));
    }

    /**
     * 修改盘点任务
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveStocktakingTask:edit')")
    @Log(title = "盘点任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArchiveStocktakingTask archiveStocktakingTask)
    {
        return toAjax(archiveStocktakingTaskService.updateArchiveStocktakingTask(archiveStocktakingTask));
    }

    /**
     * 删除盘点任务
     */
    @PreAuthorize("@ss.hasPermi('archive:ArchiveStocktakingTask:remove')")
    @Log(title = "盘点任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable String[] taskIds)
    {
        return toAjax(archiveStocktakingTaskService.deleteArchiveStocktakingTaskByTaskIds(taskIds));
    }

    /**
     * 档案入盘点任务结果上报接口
     */
    @ApiOperation("档案入盘点任务结果上报接口")
    @PostMapping("/archiveStocktakingTaskResultReport")
    @PreAuthorize("@ss.hasPermi('archive:ArchiveStocktakingTask:archiveStocktakingTaskResultReport')")
    public AjaxResult archiveStocktakingTaskResultReport(@RequestBody ArchiveStocktakingTask archiveStocktakingTask) throws JsonProcessingException, URISyntaxException {
        if (null == archiveStocktakingTask) {
            return AjaxResult.error("盘点任务信息为空，请检查重试！");
        }
        if(null==archiveStocktakingTask.getTaskId()){
            return AjaxResult.error("任务信息为空，请检查重试！");
        }
        return AjaxResult.success(archiveStocktakingTaskService.archiveStocktakingTaskResultReport(archiveStocktakingTask));
    }
}
